package io.jee.runshan.dao.impl;

import io.jee.medusa.orm.Hibernate4DaoSupport;
import io.jee.medusa.transfer.EmptyTransfer;
import io.jee.runshan.dao.IKeywordAskDao;
import io.jee.runshan.domain.KeywordAsk;
import io.jee.runshan.utils.Utils;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

@Repository
public class KeywordAskDaoImpl extends Hibernate4DaoSupport<KeywordAsk>
		implements IKeywordAskDao {

	@SuppressWarnings("unchecked")
	@Override
	public KeywordAsk findKeyword(String keyword) {
		if (Utils.isNullOrEmpty(keyword)) {
			return null;
		}
		StringBuffer hql = new StringBuffer("SELECT k.* FROM runshan_keyword_ask k WHERE k.type = 0 ");
		hql.append(" AND INSTR(:keyword , k.keyword) ");
		Query query = this.getSession().createSQLQuery(hql.toString());
		query.setParameter("keyword", keyword);
		query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
		
		if(query.list().size()>0){
			List<KeywordAsk> datas = EmptyTransfer.transfer(query.list(), KeywordAsk.class);
			if (datas.size() > 0) {
				return datas.get(0);
			}
		}
		return null;
	}
}
